import courseApi from "../../api/chaoxing/courseApi/index"
import signActivity from "../../api/chaoxing/signActivity/index"
import userApi from "../../api/chaoxing/userApi/index"
import showToast from "../../utils/toast"

// pages/course/course.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    course: [] as CourseType[],
  },
  async onTap({ currentTarget }: WechatMiniprogram.BaseEvent) {
    const course = currentTarget.dataset.courseItem as CourseType
    const params = userApi.getUser(userApi.getCurUser()) as UserCookieType
    const activity = await signActivity.aPromise(course, params.uf!, params._d!, params._uid!, params.vc3!)
    if (typeof activity === 'string') {
      showToast({
        title: "当前课程没有待签的活动",
        icon: 'none',
      })
    } else{
      const msg = await signActivity.activityAll(activity,{});
      showToast({
        title: msg,
        icon: 'none',
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const params = userApi.getUser(getApp().globalData.curUser) as UserCookieType
    courseApi.getCourses(params._uid!, params._d!, params.vc3!).then(res => {
      if (typeof res == 'string') {
        showToast({ title: '获取课程列表失败', icon: 'error' })
      }
      this.setData({ course: res as CourseType[] })
      courseApi.storeCourses(res as CourseType[]);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})